﻿Imports System.Linq

Namespace LightSwitchApplication

    Public Class Home

        Private Sub CreateNewAnimal_Execute()
            ' Write your code here.
            Me.Application.ShowCreateNewAnimal()
        End Sub

        Private Sub SearchAnimal_Execute()
            ' Write your code here.
            Me.Application.ShowAnimalsListDetail()
        End Sub

        Private Sub CreateNewPerson_Execute()
            ' Write your code here.
            Me.Application.ShowCreateNewPerson()

        End Sub

        Private Sub SearchPerson_Execute()
            ' Write your code here.
            Me.Application.ShowPersonsListDetail()

        End Sub

        Private Sub Home_InitializeDataWorkspace(saveChangesTo As List(Of IDataService))

            Dim totadopt = Me.DataWorkspace.WCF_RIA_ServiceData.Dashboards.Where(Function(e) e.Archived = 1).Execute().Count()
            'Dim totadopt = Me.DataWorkspace.ApplicationData.Animals.Where(Function(e) e.Archived = 1).Execute().Count()
            'Dim totfost = Me.DataWorkspace.ApplicationData.Animals.Where(Function(e) e.ActiveMovementType.Id = 2).Execute().Count()
            Dim totfost = Me.DataWorkspace.WCF_RIA_ServiceData.Dashboards.Where(Function(e) e.LastActiveStatus = "Foster").Execute().Count()
            Dim volun = Me.DataWorkspace.ApplicationData.Persons.Where(Function(e) e.Flag.Any(Function(a) a.PersonFlag.Flag = "Volunteer") And e.Status = Nothing)


            TotalAdoptions = "Total Adoptions: " & totadopt
            TotalFosters = "Total Fostering: " & totfost
            TotalVolunteers = "Total Volunteers: " & volun.Execute().Count()

            'persondialoghelper = New DialogHelper(Me.Persons, "PersonDelete")
        End Sub

        Private Sub DeleteAnimal_Execute()
            ' Write your code here.
            Me.OpenModalWindow("AnimalDelete")
        End Sub

        Private Sub ConfirmDelete_Execute()
            ' Write your code here.
            Save()
            Me.CloseModalWindow("AnimalDelete")
            Me.CloseModalWindow("PersonDelete")
        End Sub

        Private Sub DeletePerson_Execute()
            ' Write your code here.
            Me.OpenModalWindow("PersonDelete")
        End Sub

        'Private persondialoghelper As DialogHelper

        Private Sub EditDialogCancel_Execute()
            Me.DataWorkspace.ApplicationData.Details.DiscardChanges()
            Me.CloseModalWindow("PersonDelete")
        End Sub

        Private Sub Home_Created()
            'persondialoghelper.Initialise()
        End Sub

        Private Sub UnDeletePerson_Execute()
            ' Write your code here.
            Dim deletedDummy As Person = Me.DataWorkspace.ApplicationData.Details.GetChanges().DeletedEntities.OfType(Of Person)().FirstOrDefault(Function(d) d.Id.Equals(Persons.SelectedItem.Id))
            If deletedDummy IsNot Nothing Then
                deletedDummy.Details.DiscardChanges()
            End If
        End Sub

        Private Sub UnDeletePerson_CanExecute(ByRef result As Boolean)
            ' Write your code here.
            result = Persons.SelectedItem IsNot Nothing AndAlso Persons.SelectedItem.Details.EntityState = EntityState.Deleted
        End Sub

        Private Sub DeletePersonButton_Execute()
            ' Write your code here.
            Me.Persons.SelectedItem.Delete()
        End Sub

        Private Sub Cancel_Execute()
            ' Write your code here.
            Me.DataWorkspace.ApplicationData.Details.DiscardChanges()
            Me.CloseModalWindow("AnimalDelete")
        End Sub

        Private Sub AnimalDelete_Execute()
            ' Write your code here.
            Me.Dashboards.SelectedItem.Delete()
        End Sub

        Private Sub AnimalUnDelete_Execute()
            ' Write your code here.
            Dim deletedDummy As Animal = Me.DataWorkspace.ApplicationData.Details.GetChanges().DeletedEntities.OfType(Of Animal)().FirstOrDefault(Function(d) d.Id.Equals(Dashboards.SelectedItem.ID))
            If deletedDummy IsNot Nothing Then
                deletedDummy.Details.DiscardChanges()
            End If
        End Sub

        Private Sub AnimalUnDelete_CanExecute(ByRef result As Boolean)
            ' Write your code here.
            result = Dashboards.SelectedItem IsNot Nothing AndAlso Dashboards.SelectedItem.Details.EntityState = EntityState.Deleted
        End Sub
    End Class

End Namespace
